home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBRECALI.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  145 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbrecali.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19. #include <btree.h>
  20. #include <lseq.h>
  21.  
  22. /* local headers */
  23. #include "cbase_.h"
  24.  
  25. /*man---------------------------------------------------------------------------
  26. NAME
  27.      cbrecalign - align cbase record cursor
  28.  
  29. SYNOPSIS
  30.      #include <cbase.h>
  31.  
  32.      int cbrecalign(cbp, field)
  33.      cbase_t *cbp;
  34.      int field;
  35.  
  36. DESCRIPTION
  37.      The cbrecalign function aligns the record cursor in cbase cbp
  38.      with the key cursor for the specified field.  Other cursors are
  39.      not affected.
  40.  
  41.      cbrecalign will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       cbp is not a valid cbase pointer.
  44.      [EINVAL]       field is not a valid field number for
  45.                     cbase cbp.
  46.      [CBELOCK]      cbp is not locked.
  47.      [CBENKEY]      field is not a key.
  48.      [CBENOPEN]     cbp is not open.
  49.  
  50. SEE ALSO
  51.      cbkeyalign, cbrecfirst, cbreclast, cbrecnext, cbrecprev.
  52.  
  53. DIAGNOSTICS
  54.      Upon successful completion, a value of 0 is returned.  Otherwise,
  55.      a value of -1 is returned, and errno set to indicate the error.
  56.  
  57. ------------------------------------------------------------------------------*/
  58. #ifdef AC_PROTO
  59. int cbrecalign(cbase_t *cbp, int field)
  60. #else
  61. int cbrecalign(cbp, field)
  62. cbase_t *cbp;
  63. int field;
  64. #endif
  65. {
  66.     void *    buf    = NULL;
  67.     cbrpos_t cbrpos    = NIL;
  68.     lspos_t    lspos    = NIL;
  69.  
  70.     /* validate arguments */
  71.     if (!cb_valid(cbp)) {
  72.         errno = EINVAL;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if not open */
  77.     if (!(cbp->flags & CBOPEN)) {
  78.         errno = CBENOPEN;
  79.         return -1;
  80.     }
  81.  
  82.     /* validate arguments */
  83.     if (field < 0 || field >= cbp->fldc) {
  84.         errno = EINVAL;
  85.         return -1;
  86.     }
  87.  
  88.     /* check if field not a key */
  89.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  90.         errno = CBENKEY;
  91.         return -1;
  92.     }
  93.  
  94.     /* check if not locked */
  95.     if (!(cbp->flags & CBLOCKS)) {
  96.         errno = CBELOCK;
  97.         return -1;
  98.     }
  99.  
  100.     /* check if record cursor is null */
  101.     if (btcursor(cbp->btpv[field]) == NULL) {
  102.         /* set record cursor to null */
  103.         if (lssetcur(cbp->lsp, NULL) == -1) {
  104.             CBEPRINT;
  105.             return -1;
  106.         }
  107.         return 0;
  108.     }
  109.  
  110.     /* allocate storage for key */
  111.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  112.         CBEPRINT;
  113.         errno = CBEPANIC;
  114.         return -1;
  115.     }
  116.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  117.     if (buf == NULL) {
  118.         CBEPRINT;
  119.         errno = ENOMEM;
  120.         return -1;
  121.     }
  122.  
  123.     /* get key */
  124.     if (btgetk(cbp->btpv[field], buf) == -1) {
  125.         CBEPRINT;
  126.         free(buf);
  127.         return -1;
  128.     }
  129.  
  130.     /* get record position and place in key */
  131.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  132.     lspos = cbrpos;
  133.  
  134.     /* position record cursor */
  135.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  136.         CBEPRINT;
  137.         return -1;
  138.     }
  139.  
  140.     free(buf);
  141.     buf = NULL;
  142.  
  143.     return 0;
  144. }
  145.